<?php

/* 
 * CONTROLADOR DE NOTICIAS
 */

class	Noticias	extends	CI_Controller	{

				public	function	__construct()	{
								parent::__construct();
								$this->load->model('news_model');
								$this->load->helper('url');
				}

				public	function	index()	{
								$data['news']	=	$this->news_model->get_news();
								$data['title'] = "Listado de Noticias";
								// falta el titulo
								$this->load->view('templates/header',$data);
								$this->load->view('templates/cabecera',$data);
								$this->load->view('noticias/listado',$data);
								$this->load->view('templates/pie',$data);
								$this->load->view('templates/footer',$data);
				}

				public	function	vista($slug = '')	{
								
								$data['news']	=	$this->news_model->get_news($slug);
								if (count($data['news'])>0){
												$data['title'] = "Viendo Noticia".$data['news']['title'];
												$this->load->view('templates/header',$data);
												$this->load->view('templates/cabecera',$data);
												$this->load->view('noticias/single',$data);
												$this->load->view('templates/pie',$data);
												$this->load->view('templates/footer',$data);
								}
								else{
												show_404();
								}
				}
				
				public function add(){
								$this->load->library('form_validation');
								$data['title'] = "Añadir noticias";
									
								$this->form_validation->set_rules('title','Titulo','required');
								$this->form_validation->set_rules('text','Texto','required');
								
								if ($this->form_validation->run() === FALSE)
								{
								$this->load->view('templates/header',$data);
								$this->load->view('templates/cabecera',$data);
								$this->load->view('noticias/add',$data);
								$this->load->view('templates/pie',$data);
								$this->load->view('templates/footer',$data);
								}
								else {

												$arr_insert = array (
																"title" => $this->input->post('title'),
																"text" => $this->input->post('text'),
																"slug" => url_title($this->input->post('title'))
																);
																$this->news_model->insert($arr_insert);
												redirect('noticias');
								}
				}

}
